package com.soap.config;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
public class LoggingAspect {
	
 
	// service层的统计日志/耗时(方法所在的包)
	public static final String POINT = "execution (* com.soap.service.impl.*.*(..))";
 
	/**
	 * 统计方法执行耗时Around环绕通知
	 * @param joinPoint
	 * @return
	 */
	@Around(POINT)
	public Object loggingAround(ProceedingJoinPoint joinPoint) {
		long startTime = System.currentTimeMillis();
		// 定义返回对象、得到方法需要的参数
		Object resultData = null;
		Object[] args = joinPoint.getArgs();
		MethodSignature  signature = (MethodSignature)joinPoint.getSignature();
		try {
			// 调用钉钉接口
			resultData = joinPoint.proceed(args);
			long endTime = System.currentTimeMillis();
			log.info("======>请求[{}]接口完成,耗时:{}ms,返回:{}", signature.getMethod(),(endTime - startTime) , resultData);
		} catch (Throwable e) {
			// 记录异常信息
			long endTime = System.currentTimeMillis();
			log.error("======>请求[{}]接口异常！耗时:{}ms,异常:{}",signature.getMethod(), (endTime - startTime), e.getMessage());
			throw  new RuntimeException(e.getMessage());
		}
		return resultData;
	}
}